/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* $file:		XiaoZhi_BMP280.h
* $Date:        20-January-2025
* $Revision: 	V1
* $Project: 	XiaoFei BSP Library
* $Taobao:      https://item.taobao.com/item.htm?abbucket=17&id=798866630402
*
* -------------------------------------------------------------------- */

#ifndef __XiaoZhi_BMP280_H
#define __XiaoZhi_BMP280_H

#define BMP280_UseSPI

#ifdef BMP280_UseI2C
	#define BMP280_SDA_RCC(x)   	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_SDA				GPIOA
	#define BMP280_PIN_SDA				GPIO_Pin_12
	#define BMP280_SCL_RCC(x)   	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_SCL				GPIOA
	#define BMP280_PIN_SCL				GPIO_Pin_11
	#define BMP280_SCL(x)				do{GPIO_WriteBit(BMP280_GPIO_SCL,BMP280_PIN_SCL,(BitAction)(x));XiaoFei_Delay_us(1);}while(0)
	#define BMP280_SDA(x)				do{GPIO_WriteBit(BMP280_GPIO_SDA,BMP280_PIN_SDA,(BitAction)(x));XiaoFei_Delay_us(1);}while(0)
	#define BMP280_SDARead				GPIO_ReadInputDataBit(BMP280_GPIO_SDA,BMP280_PIN_SDA)
#endif

#ifdef BMP280_UseSPI
	#define BMP280_CS_RCC(x)   	        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_CS				GPIOA
	#define BMP280_PIN_CS				GPIO_Pin_3
	#define BMP280_SCK_RCC(x)   	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_SCK				GPIOA
	#define BMP280_PIN_SCK				GPIO_Pin_11
	#define BMP280_MOSI_RCC(x)   	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_MOSI			GPIOA
	#define BMP280_PIN_MOSI				GPIO_Pin_12
	#define BMP280_MISO_RCC(x)   	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
	#define BMP280_GPIO_MISO			GPIOA
	#define BMP280_PIN_MISO				GPIO_Pin_2
	
	#define BMP280_CS(x)				GPIO_WriteBit(BMP280_GPIO_CS,BMP280_PIN_CS,(BitAction)(x))
	#define BMP280_SCK(x)				GPIO_WriteBit(BMP280_GPIO_SCK,BMP280_PIN_SCK,(BitAction)(x))
	#define BMP280_MOSI(x)				GPIO_WriteBit(BMP280_GPIO_MOSI,BMP280_PIN_MOSI,(BitAction)(x))
	#define BMP280_MISORead				GPIO_ReadInputDataBit(BMP280_GPIO_MISO,BMP280_PIN_MISO)
#endif

#define BMP280_Address 					0x76
#define BMP280_ID 						0xD0  
#define BMP280_RESET 					0xE0   
#define BMP280_STATUS 					0xF3   
#define BMP280_CTRL_MEAS 				0xF4  
#define BMP280_CONFIG 					0xF5  
#define BMP280_DIGT 					0x88 
#define BMP280_DIGP 					0x8E 
#define BMP280_TEMP 					0xFA 
#define BMP280_PRES 					0xF7 
#define BMP280_CHIP_ID 					0x58     
#define BMP280_Ex(msb,lsb)    	        (((unsigned short)msb<<8)|((unsigned short)lsb))
	
#ifndef BMP280_TypeDef_def
#define BMP280_TypeDef_def
typedef struct 
{
	unsigned short dig_t1;      /*!< The important data from BMP280 */
	signed short dig_t2;        /*!< The important data from BMP280 */
	signed short dig_t3;        /*!< The important data from BMP280 */
	unsigned short dig_p1;      /*!< The important data from BMP280 */
	signed short dig_p2;        /*!< The important data from BMP280 */
	signed short dig_p3;        /*!< The important data from BMP280 */
	signed short dig_p4;        /*!< The important data from BMP280 */
	signed short dig_p5;        /*!< The important data from BMP280 */
	signed short dig_p6;        /*!< The important data from BMP280 */
	signed short dig_p7;        /*!< The important data from BMP280 */
	signed short dig_p8;        /*!< The important data from BMP280 */
	signed short dig_p9;        /*!< The important data from BMP280 */
}BMP280_TypeDef;
#endif

#ifndef BMP280_StandBytDef_def
#define BMP280_StandBytDef_def
typedef enum
{
	BMP280_Sta1ms=0x00 ,        /*!< Set the standard sampling interval of BMP280 to 1ms */
	BMP280_Sta62ms=0x01 ,       /*!< Set the standard sampling interval of BMP280 to 62ms */
	BMP280_Sta125ms=0x02 ,      /*!< Set the standard sampling interval of BMP280 to 125ms */
	BMP280_Sta250ms=0x03 ,      /*!< Set the standard sampling interval of BMP280 to 250ms */
	BMP280_Sta500ms=0x04 ,      /*!< Set the standard sampling interval of BMP280 to 500ms */
	BMP280_Sta1000ms=0x05 ,	    /*!< Set the standard sampling interval of BMP280 to 1000ms */
	BMP280_Sta2000ms=0x06 ,     /*!< Set the standard sampling interval of BMP280 to 2000ms */
	BMP280_Sta4000ms=0x07 ,	    /*!< Set the standard sampling interval of BMP280 to 4000ms */
}BMP280_StandBytDef;
#endif

#ifndef BMP280_FilterDef_def
#define BMP280_FilterDef_def
typedef enum
{
	BMP280_Filter0=0x00 ,       /*!< Turn off the filter */
	BMP280_Filter2=0x01 ,       /*!< Set the filter to 2-order filtering */
	BMP280_Filter4=0x02 ,       /*!< Set the filter to 4-order filtering */
	BMP280_Filter8=0x03 ,       /*!< Set the filter to 8-order filtering */
	BMP280_Filter16=0x04 ,      /*!< Set the filter to 16-order filtering */
}BMP280_FilterDef;
#endif

#ifndef BMP280_MoodDef_def
#define BMP280_MoodDef_def
typedef enum
{
	BMP280_Sleep=0x00 ,         /*!< Set BMP280 to sleep mode */
	BMP280_Single=0x01 ,        /*!< Set BMP280 to single measurement mode */
	BMP280_Continuous=0x02 ,    /*!< Set BMP280 to continuous measurement mode */
}BMP280_MoodDef;
#endif

/* 
* @function: 	BMP280_GetID
* @return:		Device ID number of BMP280 device.
*/
unsigned char BMP280_GetID(void);

/* 
* @function: 	BMP280_GetDevice
* @parameter:	[out] BMP280				The important device data from BMP280 device.
*/
void BMP280_GetDevice(BMP280_TypeDef* BMP280);

/* 
* @function: 	BMP280_SetuOverSamp
* @parameter:	[out] osrs_p				The over sampling data to BMP280 device.
* @parameter:	[out] osrs_t				The over sampling data to BMP280 device.
*/
void BMP280_SetuOverSamp(unsigned char osrs_p, unsigned char osrs_t);

/* 
* @function: 	BMP280_SetConfige
* @parameter:	[out] Standbyt				The standard sampling interval to BMP280.
* @parameter:	[out] filter				The standard filter order to BMP280.
*/
void BMP280_SetConfige(BMP280_StandBytDef Standbyt, BMP280_FilterDef filter);

/* 
* @function: 	BMP280_SetMode
* @parameter:	[out] mode				    The working mode to BMP280.
*/
void BMP280_SetMode(BMP280_MoodDef mode);

/* 
* @function: 	BMP280_Check
* @parameter:	[out] BMP280				The BMP280 device will be initialized.
* @return:		Return 0x00 when the BMP280 device is initialized.
*/
unsigned char BMP280_Check(BMP280_TypeDef* BMP280);

/* 
* @function: 	BMP280_Init
* @parameter:	[out] BMP280				The BMP280 device will be initialized.
*/
void BMP280_Init(BMP280_TypeDef* BMP280);

/* 
* @function: 	BMP280_GetTemp
* @parameter:	[in] BMP280				    The BMP280 device will be initialized.
* @parameter:	[out] t_fine				Auxiliary data for function BMP280_GetPres.
* @return:		Temperature measurement results from BMP280.
*/
double BMP280_GetTemp(BMP280_TypeDef *BMP280, int* t_fine);

/* 
* @function: 	BMP280_GetPres
* @parameter:	[in] BMP280				    The BMP280 device will be initialized.
* @parameter:	[in] t_fine				    Auxiliary data from function BMP280_GetTemp.
* @return:		Air pressure measurement results from BMP280.
*/
double BMP280_GetPres(BMP280_TypeDef *BMP280, int* t_fine);

#endif


